<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入项目编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择项目状态">
          <el-option
            v-for="item in projectStatusList"
            :key="item.key"
            :label="item.label"
            :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['workOrder:project:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--<el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">-->
    <el-table v-loading="loading" :data="projectList">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <!--<el-table-column label="主键id" align="center" prop="id" />-->
      <el-table-column label="序号" type="index" width="50" align="center"/>
      <el-table-column label="项目编码" align="center" prop="code" />
      <el-table-column label="项目名称" align="center" prop="name" min-width="250"/>
      <el-table-column label="项目令号" align="center" prop="orderNum" width="100"/>
      <el-table-column label="项目状态" align="center" prop="status" >
        <template slot-scope="scope">
          <span v-if="scope.row.status == '0'">
            <el-tag>未启动</el-tag>
          </span>
          <span v-else-if="scope.row.status == '1'">
            <el-tag type="warning">执行中</el-tag>
          </span>
          <span v-else-if="scope.row.status == '2'">
            <el-tag type="success">已完成</el-tag>
          </span>
          <span v-else-if="scope.row.status == '3'">
            <el-tag type="danger">关闭</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="监控token" align="center" prop="token" width="340">
      </el-table-column>
      <el-table-column label="项目开始时间" align="center" prop="startTime" width="130">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目结束时间" align="center" prop="endTime" width="130">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="230">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['workOrder:project:view']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['workOrder:project:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="projectExportBefore(scope.row.code)"
            v-hasPermi="['workOrder:project:export']"
          >项目导出</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['workOrder:project:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="项目编码" prop="code">
          <div v-if="typeMark == 'add'">
            <el-input v-model="form.code" placeholder="请输入项目编码"/>
          </div>
          <div v-else>{{form.code}}</div>
        </el-form-item>
        <el-form-item label="项目名称" prop="name">
          <div v-if="typeMark != 'view'">
            <el-input v-model="form.name" placeholder="请输入项目名称" />
          </div>
          <div v-else>{{form.name}}</div>
        </el-form-item>
        <el-form-item label="项目令号" prop="orderNum">
          <div v-if="typeMark != 'view'">
            <el-input v-model="form.orderNum" placeholder="请输入项目令号" />
          </div>
          <div v-else>{{form.orderNum}}</div>
        </el-form-item>
        <el-form-item label="项目状态" prop="status">
          <div v-if="typeMark != 'view'">
            <el-select v-model="form.status" placeholder="请选择项目状态" class="width_100">
              <el-option
                v-for="item in projectStatusList"
                :key="item.key"
                :label="item.label"
                :value="item.key">
              </el-option>
            </el-select>
          </div>
          <div v-else>{{form.status == '0' ? '未启动' : form.status == '1' ? '执行中' : form.status == '2' ? '已完成' : form.status == '3' ? '关闭' : ''}}</div>
        </el-form-item>
        <el-form-item label="项目开始时间" prop="startTime">
          <div v-if="typeMark != 'view'">
            <el-date-picker clearable
              v-model="form.startTime"
              type="date"
              class="width_100"
              value-format="yyyy-MM-dd"
              :picker-options="pickerOptionsStart"
              @change="startTimeStatus"
              placeholder="请选择项目开始时间">
            </el-date-picker>
          </div>
          <div v-else>{{form.startTime}}</div>
        </el-form-item>
        <el-form-item label="项目结束时间" prop="endTime">
          <div v-if="typeMark != 'view'">
            <el-date-picker clearable
              v-model="form.endTime"
              type="date"
              class="width_100"
              value-format="yyyy-MM-dd"
              :picker-options="pickerOptionsEnd"
               @change="endTimeStatus"
              placeholder="请选择项目结束时间">
            </el-date-picker>
          </div>
          <div v-else>{{form.endTime}}</div>
        </el-form-item>
        <el-form-item label="回款状态" prop="receipt">
          <div v-if="typeMark != 'view'">
            <el-input v-model="form.receipt" placeholder="请输入回款状态" />
          </div>
          <div v-else>{{form.receipt}}</div>
        </el-form-item>
        <el-form-item label="项目团队" prop="teams">
          <div v-if="typeMark != 'view'">
            <el-select v-model="form.userProjectList" multiple placeholder="请选择项目团队" class="width_100">
              <el-option
                v-for="item in teamsList"
                :key="item.userId"
                :label="item.nickName"
                :value="item.userId"
              ></el-option>
            </el-select>
          </div>
          <div v-else>{{teamsViewList.join(',')}}</div>
        </el-form-item>

        <el-form-item label="监控token" prop="token" disabled="true">
          <div>{{form.token}}</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="typeMark != 'view'">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>
  .width_100 {
    width: 100%;
  }
</style>

<script>
import { listProject, getProject, delProject, addProject, updateProject, queryTeamsList
  ,projectExport,downloadProject} from "@/api/workOrder/project";

export default {
  name: "Project",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目信息表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        orderNum: null,
        status: null,
        startTime: null,
        endTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
          code: [
              { required: true, message: "项目编码不能为空", trigger: "blur" }
          ],
          name: [
              { required: true, message: "项目名称不能为空", trigger: "blur" }
          ]
      },
      projectStatusList: [
          {
              key: '0',
              label: '未启动'
          },
          {
              key: '1',
              label: '执行中'
          },
          {
              key: '2',
              label: '已完成'
          },
          {
              key: '3',
              label: '关闭'
          }
      ],
      typeMark: 'add',
      teamsList: [],//项目团队列表
      teamsViewList: [],
      pickerOptionsStart: {
          disabledDate: time => {
              let endDateVal = this.form.endTime;
              if (endDateVal) {
                  return time.getTime() > new Date(endDateVal).getTime()
              }
          }
      },
      pickerOptionsEnd: {
          disabledDate: time => {
              let beginDateVal = this.form.startTime;
              if (beginDateVal) {
                  return time.getTime() < new Date(beginDateVal).getTime()
              }
          }
      },
    };
  },
  created() {
    this.getList();
    //项目团队列表
    this.getTeamsList();
  },
  methods: {
    // 时间开始选择器
    startTimeStatus(value) {
        this.form.startTime = value;
    },
    // 时间结束选择器
    endTimeStatus(value) {
        this.form.endTime = value;
    },
    /** 项目团队列表 **/
    getTeamsList() {
        queryTeamsList().then(res => {
            this.teamsList = res.data;
        });
    },
    /** 查询项目信息列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then(response => {
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        orderNum: null,
        status: null,
        startTime: null,
        endTime: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
   /* // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },*/
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目信息";
      this.typeMark = 'add';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProject(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目信息";
        this.typeMark = 'edit';
      });
    },
    //查看按钮
    handleView(row) {
        this.reset();
        const id = row.id || this.ids;
        getProject(id).then(response => {
            this.form = response.data;
            this.teamsViewList = [];

            this.teamsList.forEach((d) => {
                if(this['form']['userProjectList'].indexOf(d.userId) != '-1') {
                    this.teamsViewList.push(d.nickName);
                }
            });
            this.open = true;
            this.title = "查看项目信息";
            this.typeMark = 'view';
        });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProject(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProject(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('删除项目将同时删除项目内数据库数据及文档附件类数据。请在执行删除操作前确认数据已导出并妥善保管。是否确认删除项目信息中所选的数据项？').then(function() {
        return delProject(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('workOrder/project/export', {
        ...this.queryParams
      }, `project_${new Date().getTime()}.xlsx`)
    },
  /** 项目导出操作 */
    projectExport(projectCode) {
      console.log("调用")
    this.downloadNew('/workOrder/project/projectExport/'+projectCode, ''
      , `项目导出_${new Date().getTime()}.rar`).then(res => {
      console.log("sucess")
    });
    },
    projectExportBefore(projectCode){
      this.$modal.confirm('项目导出会将本项目下全部附件整体打包下载，需要较长处理时间，是否打包？').then(res => {
        return this.projectExport(projectCode);
      }).then(() => {
        // this.$modal.msgSuccess("导出成功");
      }).catch(() => {});
    }
  }
};
</script>
